package com.apache.bid.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidItemGroup;
import com.apache.bid.manager.BidItemGroupManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;

public class BidItemGroupManagerImpl implements BidItemGroupManager {

	protected IDao bidItemGroupDao;
	protected final String entityName = "com.apache.bid.entity.BidItemGroup";

	public void setBidItemGroupDao(IDao bidItemGroupDao) {
		this.bidItemGroupDao = bidItemGroupDao;
	}

	public String saveInfo(ParamsVo<BidItemGroup> vo) throws BusinessException {
		BidItemGroup bidItemGroup = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidItemGroup_"+infoId;
		if (Validator.isNull(bidItemGroup.getGroupId())) {
			bidItemGroup.setGroupId(infoId);
		}
		if (Constant.getInstance().ifWriteDatabase()) {
			bidItemGroup.setCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			MethodParam param = new MethodParam("BidItemGroup", cacheKey, "", entityName);
			param.setVaule(bidItemGroup);
			if (bidItemGroupDao.insert(param)) {
				BidCacheFactory.getInstance().getCacheManager("itemGroup").createCacheObject(bidItemGroup);
				return bidItemGroup.getGroupId();
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			if (!"default".equals(bidItemGroup.getGroupCode())) {
				vo.setParams("methodCode", "saveItemGroup");
			}
			return bidItemGroup.getGroupId();
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidItemGroup> vo) throws BusinessException {
		BidItemGroup bidItemGroup = vo.getObj();
		if (Constant.getInstance().ifWriteDatabase()) {
			if (Validator.isNotNull(bidItemGroup.getGroupId())) {
				String cacheKey = "";
				//String cacheKey="bidItemGroup_"+bidItemGroup.getGroupId();
				MethodParam param = new MethodParam("BidItemGroup", cacheKey, "", entityName);
				bidItemGroup.setModifyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
				param.setVaule(bidItemGroup);
				boolean mark = bidItemGroupDao.edit(param);
				if (mark) {
					param.setKey("ById");
					param.setInfoId(bidItemGroup.getGroupId());
					bidItemGroup = (BidItemGroup) bidItemGroupDao.selectById(param);
					BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("itemGroup");
					if (cache.checkCacheObject(cache.item_group_prefix + bidItemGroup.getSessionId())) {
						cache.createCacheObject(bidItemGroup);
					}
				}
				return mark;
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			vo.setParams("methodCode", "editItemGroup");
			return true;
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidItemGroup> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidItemGroup_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		if (Constant.getInstance().ifWriteDatabase()) {
			if (Validator.isNull(infoId)) {
				String sessionId = String.valueOf(vo.getParams("sessionId"));
				if (Validator.isNotNull(sessionId)) {
					MethodParam param = new MethodParam("BySessionId", cacheKey, "", entityName);
					param.setParams("sessionId", sessionId);
					param.setDelete(true);
					return bidItemGroupDao.delete(param);
				}
				return false;
			}
			MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
			if ("false".equals(mark)) {//逻辑删除
				param.setInfoId(infoId);
				BidItemGroup info = (BidItemGroup) bidItemGroupDao.selectById(param);
				if (Validator.isEmpty(info)) {
					return false;
				}
				param.setVaule(info);//此处需要先将状态值赋值为删除状态
				param.setKey("BidItemGroup");
				return bidItemGroupDao.edit(param);
			} else {
				param.setParams("groupId", infoId);
				param.setDelete(true);
				return bidItemGroupDao.delete(param);
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			if ("false".equals(mark)) {//逻辑删除
			} else {
			}
			vo.setParams("methodCode", "deleteItemGroup");
			return true;
		}
	}

	public Object getInfoById(ParamsVo<BidItemGroup> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidItemGroup_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidItemGroupDao.selectById(param);
	}

	public Object execute(ParamsVo<BidItemGroup> vo) {
		String methodKey = vo.getMethodKey();
		if ("checkByCodeOrName".equalsIgnoreCase(methodKey)) {
			return this.checkByCodeOrName(vo.getObj());
		}
		return null;
	}

	//检查指定sessionId下标的分组是否存在
	private Map<String, String> checkByCodeOrName(BidItemGroup group) {
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("flag", "true");
		resultMap.put("msg", "编号和名称可用！");
		MethodParam param = new MethodParam("ByCodeOrName", "", "", entityName);
		StringBuffer sbCode = new StringBuffer();
		StringBuffer sbName = new StringBuffer();
		if (Validator.isNotNull(group.getSessionId()) && !"0".equals(group.getSessionId())) {
			sbCode.append(" and sessionId =:sessionId");
			sbName.append(" and sessionId =:sessionId");
			param.setParams("sessionId", group.getSessionId());
		}

		if (Validator.isNotNull(group.getGroupCode())) {
			sbCode.append(" and groupCode =:groupCode");
			param.setParams("groupCode", group.getGroupCode());//分组编号不能重复[同sessionId内]

		}
		param.setSqlStr(bidItemGroupDao.getSql(3) + sbCode.toString());
		long countGcode = bidItemGroupDao.count(param);

		param.setParams("groupCode", "");
		if (Validator.isNotNull(group.getGroupName())) {
			sbName.append(" and groupName =:groupName");
			param.setParams("groupName", group.getGroupName());//分组编号不能重复[同sessionId内]

		}
		param.setSqlStr(bidItemGroupDao.getSql(3) + sbName.toString());
		long countGname = bidItemGroupDao.count(param);
		if (countGcode > 0 && countGname > 0) {
			resultMap.put("flag", "false");
			resultMap.put("msg", "分组编号和分组名称均已经存在！");
		} else if (countGcode > 0) {
			resultMap.put("flag", "false");
			resultMap.put("msg", "分组编号重复了！");
		} else if (countGname > 0) {
			resultMap.put("flag", "false");
			resultMap.put("msg", "分组名称重复了！");
		}
		return resultMap;
	}

	public Page getPageInfo(ParamsVo<BidItemGroup> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		//param.setOrderby(" create_time desc");
		String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by open_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidItemGroupDao.pageSelect(param);
		return page;
	}

	public List<BidItemGroup> getList(ParamsVo<BidItemGroup> vo) {
		MethodParam param = setMethodParams(vo, 2);
		//param.setOrderby(" create_time desc");
		String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by open_time desc ");
		param.setParams("orderBy", orderBy);
		return bidItemGroupDao.select(param);
	}

	public long countInfo(ParamsVo<BidItemGroup> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidItemGroupDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidItemGroup> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidItemGroupDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidItemGroup bidItemGroup = vo.getObj();
		param.setResultCache(false);
		if (Validator.isEmpty(bidItemGroup))
			return param;
		if (Validator.isNotNull(bidItemGroup.getGroupCode())) {
			sb.append(" and groupCode = :groupCode");
			param.setParams("groupCode", bidItemGroup.getGroupCode());
		}
		if (Validator.isNotNull(bidItemGroup.getGroupName())) {
			sb.append(" and groupName like '%" + bidItemGroup.getGroupName() + "%' ");
			param.setParams("groupName", " like '%" + bidItemGroup.getGroupName() + "%' ");
		}
		if (Validator.isNotNull(bidItemGroup.getSessionId())) {
			sb.append(" and sessionId = :sessionId");
			param.setParams("sessionId", bidItemGroup.getSessionId());
		}
		if (Validator.isNotNull(bidItemGroup.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", bidItemGroup.getCreateTime());
		}
		if (Validator.isNotNull(bidItemGroup.getModifyTime())) {
			sb.append(" and modifyTime = :modifyTime");
			param.setParams("modifyTime", bidItemGroup.getModifyTime());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidItemGroup.getSpare1());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidItemGroup.getSpare2());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidItemGroup.getSpare3());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidItemGroup.getSpare4());
		}
		if (Validator.isNotNull(bidItemGroup.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidItemGroup.getSpare5());
		}
		if (Validator.isNotNull(bidItemGroup.getOpenTime())) {
			sb.append(" and openTime = :openTime");
			param.setParams("openTime", bidItemGroup.getOpenTime());
		}
		param.setSqlStr(sb.toString());
		return param;
	}

}
